using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.FaceBook
{
	public class FBContactMgr
	{
		public const int STATUS_GETTING = 1;
		public const int STATUS_SYNC_COMPLETE = 2;
		private const string TAG = "FBContactMgr";
		private static int mStatus;
		private EventWatcher mWatcherGetFBComplete;
		public bool getFBContact(bool isRefresh = false)
		{
			if (!isRefresh)
			{
				double @double = ConfigMgr.getDouble("lastGetFBContactime", 0.0);
				double nowMilliseconds = Util.getNowMilliseconds();
				if (nowMilliseconds - @double < 86400000.0 && nowMilliseconds - @double > 0.0)
				{
					return false;
				}
			}
			if (FBContactMgr.mStatus == 1)
			{
				return false;
			}
			FBContactMgr.mStatus = 1;
			if (this.mWatcherGetFBComplete == null)
			{
				this.mWatcherGetFBComplete = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventGetFbContactComplete));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, this.mWatcherGetFBComplete);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETFRIEND_FAIL, this.mWatcherGetFBComplete);
			}
			NetSceneGetMFriend netSceneGetMFriend = new NetSceneGetMFriend();
			netSceneGetMFriend.doScene(1);
			return true;
		}
		private void onEventGetFbContactComplete(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_GETFRIEND_SUCCESS || evtArgs.mEventID == EventConst.ON_NETSCENE_GETFRIEND_FAIL)
			{
				EventCenter.removeEventWatcher(evtArgs.mEventID, this.mWatcherGetFBComplete);
				this.mWatcherGetFBComplete = null;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_GETFRIEND_SUCCESS)
			{
				long num = (long)evtArgs.mObject;
				if (num > 0L)
				{
					StorageMgr.fbContact.delByUpdateTime(num);
				}
				ConfigMgr.setDouble("lastGetFBContactime", Util.getNowMilliseconds());
			}
			FBContactMgr.mStatus = 2;
		}
		private static void sycFriendContact()
		{
			List<FBContact> list = StorageMgr.fbContact.getList();
			if (list != null && list.get_Count() > 0)
			{
				using (List<FBContact>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						FBContact current = enumerator.get_Current();
						if (current != null)
						{
							Contact contact = StorageMgr.contact.method_0(current.strUsrName);
							if (contact != null && ContactMgr.isContact(contact))
							{
								current.strNickName = contact.strNickName;
								current.strProvince = contact.strProvince;
								current.strCity = contact.strCity;
								current.strSignature = contact.strSignature;
								current.nSex = contact.nSex;
								current.nPersonalCard = contact.nPersonalCard;
								current.nFlag = contact.nFlags;
								current.nStatus = 2;
							}
							else
							{
								if (contact != null)
								{
									current.nStatus = 1;
								}
								else
								{
									current.nStatus = 0;
								}
							}
						}
					}
				}
				StorageMgr.fbContact.updateList(list);
			}
		}
		public static bool addInBlackList(FBContact contact)
		{
			if (string.IsNullOrEmpty(contact.strUsrName))
			{
				return false;
			}
			bool result;
			try
			{
				Contact contact2 = StorageMgr.contact.method_0(contact.strUsrName);
				if (contact2 == null)
				{
					contact2 = FBContactMgr.ConvertToContact(contact);
					StorageMgr.contact.vmethod_0(contact2);
				}
				ContactMgr.operationSetBlackList(contact2, true);
				contact.nFlag |= 8u;
				StorageMgr.fbContact.update(contact);
				return true;
			}
			catch (Exception ex)
			{
				Log.smethod_1("FBContactMgr", ex.ToString());
				result = false;
			}
			return result;
		}
		public static bool moveOutBlackList(FBContact contact)
		{
			Contact contact2 = StorageMgr.contact.method_0(contact.strUsrName);
			if (contact2 == null)
			{
				Log.smethod_1("FBContactMgr", "Can not move out BlackList, the contact is not exist");
				return false;
			}
			ContactMgr.operationSetBlackList(contact2, false);
			contact.nFlag &= 4294967287u;
			StorageMgr.fbContact.update(contact);
			return true;
		}
		public static bool isInBlackList(FBContact contact)
		{
			return (contact.nFlag & 8u) > 0u;
		}
		public static Contact ConvertToContact(FBContact fbContact)
		{
			if (string.IsNullOrEmpty(fbContact.strUsrName))
			{
				return null;
			}
			Contact contact = new Contact();
			contact.nFlags = 0u;
			contact.strUsrName = fbContact.strUsrName;
			contact.strSignature = fbContact.strSignature;
			contact.strQuanPin = fbContact.strNickNameQuanPin;
			contact.strPYInitial = fbContact.strNickNamePYInitial;
			contact.strMobile = fbContact.strMobile;
			contact.strNickName = fbContact.strNickName;
			contact.strEmail = fbContact.strEmail;
			contact.strCity = fbContact.strCity;
			contact.strProvince = fbContact.strProvince;
			contact.nPersonalCard = fbContact.nPersonalCard;
			contact.nFlags |= 2u;
			contact.nSnsFlag = fbContact.nSnsFlag;
			return contact;
		}
	}
}
